<template>
  <div class="home-container" ref="container" @wheel="handleWheel" v-loading="loading">
    <ul 
      class="carousel-container"
      :style="{
        marginTop
      }"
      @transitionend="handleTransitionEnd"
      >
      <li v-for="item in data" :key="item.id">
        <CarouselItem :carousel="item"></CarouselItem>
      </li>
    </ul>
    <div v-show="index > 0" @click="switchTo(index - 1)" class="icon icon-up">
      <Icon type="arrowUp"></Icon>
    </div>
    <div v-show="index < data.length - 1" @click="switchTo(index + 1)" class="icon icon-down">
      <Icon type="arrowDown"></Icon>
    </div>
    <ul class="indicator">
      <li :class="{active: i === index}" v-for="(item, i) in data" :key="item.id" @click="switchTo(i)"></li>
    </ul>
    <div v-loading></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import CarouselItem from "./CarouselItem"
import Icon from "@/components/Icon"

export default {
  components: {
    CarouselItem,
    Icon
  },
  data() {
    return {
      index: 0,
      containerHeight: 0,
      switching: false
    }
  },
  methods: {
    switchTo(i) {
      this.index = i
    },
    handleWheel(e) {
      if (this.switching) {
        return
      }
      if (e.deltaY < -5 && this.index > 0) {
        this.switching = true
        this.index--
      } else if (e.deltaY > 5 && this.index < this.data.length - 1) {
        this.switching = true
        this.index++
      }
    },
    handleTransitionEnd() {
      this.switching = false
    },
    handleResize() {
      this.containerHeight = this.$refs.container.clientHeight;
    },
  },
  mounted() {
    this.containerHeight = this.$refs.container.clientHeight
    window.addEventListener("resize", this.handleResize)
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.handleResize)
  },
  computed: {
    marginTop() {
      return -this.index * this.containerHeight + "px"
    },
    ...mapState("banner", ["loading", "data"])
  },
  created() {
    this.$store.dispatch("banner/fetchBanner")
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/var.scss";
.home-container {
  height: 100%;
  width: 100%;
  position: relative;
  overflow: hidden;
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  .carousel-container {
    width: 100%;
    height: 100%;
    transition: 500ms;
    li {
      width: 100%;
      height: 100%;
    }
  }
  .icon {
    position: absolute;
    font-size: 30px;
    $gap: 24px;
    color: $gray;
    cursor: pointer;
    left: 50%;
    transform: translateX(-50%);
    &.icon-up {
      top: $gap;
      animation: jump-up 2s infinite;
    }
    &.icon-down {
      bottom: $gap;
      animation: jump-down 2s infinite;
    }
    $jump: 4px;
    @keyframes jump-up {
      0% {
        transform: translateY($jump);
      }
      50% {
        transform: translateY(-$jump);
      }
      100% {
        transform: translateY($jump);
      }
    }
    @keyframes jump-down {
      0% {
        transform: translateY(-$jump);
      }
      50% {
        transform: translateY($jump);
      }
      100% {
        transform: translateY(-$jump);
      }
    }
  }
  .indicator {
    position: absolute;
    transform: translateY(-50%);
    top: 50%;
    right: 20px;
    li {
      width: 8px;
      height: 8px;
      border-radius: 50%;
      background-color: $words;
      cursor: pointer;
      margin-bottom: 10px;
      border: 1px solid #FFF;
      box-sizing: border-box;
      &.active {
        background-color: #FFF;
      }
    }
  }
}
</style>